package com.student.management.root.dao.impl;

import com.student.management.root.bean.Menu;
import com.student.management.root.dao.MiddleDao;
import com.student.management.root.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MiddleDaoImpl extends DBUtils implements MiddleDao {
    @Override
    public int insertMiddle(int roleid, String[] ids) {
        int k = 0;
        try {
            String sql = "insert into middle values(null,?,?)";
            //批量新增
            pps = getPps(sql);
            for (String id : ids) {
                pps.setInt(1, roleid);
                pps.setString(2, id);
                pps.addBatch();
            }
            pps.executeBatch();
            k = 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return k;
    }

    @Override
    public int deleteMiddle(int roleid) {
        int delete = 0;
        String sql = "delete from middle where roleid=?;";
        List params = new ArrayList<>();
        params.add(roleid);
        delete = update(sql, params);
        return delete;
    }

    @Override
    public ArrayList<Integer> queryMiddleById(int roleid) {
        ArrayList<Integer> middleById = new ArrayList<>();
        try {
            String sql = "select * from middle where roleid = ?;";
            List params = new ArrayList<>();
            params.add(roleid);
            resultSet = query(sql, params);
            while (resultSet.next()){

                middleById.add(resultSet.getInt("menuid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return middleById;
    }


}
